package com.thymeleaf.demo.myinterceptor;

import com.alibaba.fastjson.JSONObject;

import com.thymeleaf.demo.model.MallAdmin;
import com.thymeleaf.demo.util.result.ResultEnum;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;

@Component
public class AdminInterceptor implements HandlerInterceptor {

    //在请求处理之前进行调用（Controller方法调用之前
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        System.out.printf("preHandle被调用");

        httpServletResponse.setCharacterEncoding("UTF8");
        // 获取session
        HttpSession session = httpServletRequest.getSession();
        // 判断session中有无admin对象
        MallAdmin admin = (MallAdmin) session.getAttribute("admin");

        if(admin == null){
            httpServletRequest.setAttribute("errorMsg", ResultEnum.LOGIN_ERROR.getMsg());
            httpServletRequest.getRequestDispatcher("/mall/admin/login").forward(httpServletRequest, httpServletResponse);
            return false;
        }

        return true;    //如果false，停止流程，api被拦截
    }

    //请求处理之后进行调用，但是在视图被渲染之前（Controller方法调用之后）
    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle被调用");
    }

    //在整个请求结束之后被调用，也就是在DispatcherServlet 渲染了对应的视图之后执行（主要是用于进行资源清理工作）
    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
        System.out.println("afterCompletion被调用");
    }

}
